package com.frejoys.common.util;

import cn.hutool.core.lang.Singleton;
import cn.hutool.core.lang.Snowflake;
import com.frejoys.common.config.ValueConfig;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@RequiredArgsConstructor
public class SnowflakeUtil {

    private final ValueConfig valueConfig;

    private static Snowflake appSnowflake;
    // private static Snowflake adminSnowflake;
    // private static Snowflake storeSnowflake;
    // private static Snowflake agentSnowflake;
    // private static Snowflake businessSnowflake;

    @PostConstruct
    private void init() {

        appSnowflake = Singleton.get(Snowflake.class, new Date(valueConfig.getAppSnowflakeTimestamp()), valueConfig.getAppSnowflakeWorkerId(), valueConfig.getAppSnowflakeDatacenterId(), true);

        // adminSnowflake = Singleton.get(Snowflake.class, new Date(valueConfig.getAdminSnowflakeTimestamp()), valueConfig.getAdminSnowflakeWorkerId(), valueConfig.getAdminSnowflakeDatacenterId(), true);
        // storeSnowflake = Singleton.get(Snowflake.class, new Date(valueConfig.getStoreSnowflakeTimestamp()), valueConfig.getStoreSnowflakeWorkerId(), valueConfig.getStoreSnowflakeDatacenterId(), true);
        // agentSnowflake = Singleton.get(Snowflake.class, new Date(valueConfig.getAgentSnowflakeTimestamp()), valueConfig.getAgentSnowflakeWorkerId(), valueConfig.getAgentSnowflakeDatacenterId(), true);
        // businessSnowflake = Singleton.get(Snowflake.class, new Date(valueConfig.getBusinessSnowflakeTimestamp()), valueConfig.getBusinessSnowflakeWorkerId(), valueConfig.getBusinessSnowflakeDatacenterId(), true);
    }

    public static synchronized long appNextId() {
        return appSnowflake.nextId();
    }

    // public static long adminNextId() {
    //     return adminSnowflake.nextId();
    // }
    //
    // public static long storeNextId() {
    //     return storeSnowflake.nextId();
    // }
    //
    // public static long agentNextId() {
    //     return agentSnowflake.nextId();
    // }
    //
    // public static long businessNextId() {
    //     return businessSnowflake.nextId();
    // }

}
